package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/5/5.
 */

import com.sun.corba.se.pept.transport.ReaderThread;
import com.sun.javafx.collections.MappingChange;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xianzhiOA.bean.XzApprovalProcess;
import com.xianzhiOA.bean.XzRole;
import com.xianzhiOA.bean.XzTempletBase;
import com.xianzhiOA.bean.XzTempletExtend;
import com.xianzhiOA.service.XzApprovalProcessService;
import com.xianzhiOA.service.XzRoleService;
import com.xianzhiOA.service.XzTempletBaseService;
import com.xianzhiOA.service.XzTempletExtendService;
import com.xianzhiOA.util.PageBean;
import org.apache.ibatis.annotations.ResultMap;
import org.hibernate.validator.internal.constraintvalidators.ModCheckBase;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.lang.ref.PhantomReference;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王诗博
 * @create 2017-05-05 10:54
 **/
@Controller
@RequestMapping("/XzTempletBase")
public class XzTempletBaseController {
    @Autowired
    private XzTempletBaseService xzTempletBaseService;
    @Autowired
    private XzRoleService RoleService;
    @Autowired
    private XzTempletExtendService templetExtendService;
    @Autowired
    private XzApprovalProcessService approvalProcessService;
    //分页显示所有模板
    @RequestMapping("/selTempletAll.emp")
    public ModelAndView selTempletAll(XzTempletBase xzTempletBase, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows){
        ModelAndView mv=new ModelAndView("backEnd/templetShow");
        PageBean<XzTempletBase> pageBean= xzTempletBaseService.selAll(xzTempletBase,page,rows);
        List<XzTempletBase> templetBaseList=pageBean.getList();
        mv.addObject("templetBaseList",templetBaseList);
        mv.addObject("xzTempletBase", xzTempletBase);
        mv.addObject("page",pageBean.getPageNum());
        mv.addObject("pages",pageBean.getPages());
        mv.addObject("rows",pageBean.getSize());
        mv.addObject("total",pageBean.getTotal());
        return mv;
    }

    //跳转到添加模板页面  toCreatTemplet.emp
    @RequestMapping("/toTempletCreatPage.emp")
    public ModelAndView toTempletCreatPage(){
        ModelAndView mv=new ModelAndView("backEnd/templetCreate");

       // mv.addObject("xzTempletExtend",new XzTempletExtend());
        mv.addObject("xzTempletBase",new XzTempletBase());
        //查询所有角色赋给select框
        XzRole role=new XzRole();
        List<XzRole> roleslist=RoleService.selRoleAll(role);
        mv.addObject("roleslist",roleslist);
        mv.addObject("editType","add");
        return mv;
    }

    //删除模板（同时删除三个表中的数据）
    @RequestMapping("/deleteTemplet.emp")
    public ModelAndView deleteTemplet(Long templetBaseId){
        ModelAndView mv=new ModelAndView("redirect:selTempletAll.emp");
        Date newtime = new Date();
        //删除基础表
        XzTempletBase xzTempletBase=new XzTempletBase();
        xzTempletBase.setTempletBaseId(templetBaseId);
        xzTempletBase.setDeleteTime(newtime);
        int a=xzTempletBaseService.deleteByPrimaryKey(xzTempletBase);
        //删除扩展表
        XzTempletExtend xzTempletExtend=new XzTempletExtend();
        xzTempletExtend.setTempletBaseId(templetBaseId);
        xzTempletExtend.setDeleteTime(newtime);
        int b=templetExtendService.deleteExtendByKey(xzTempletExtend);
        //删除节点表
        XzApprovalProcess xzApprovalProcess=new XzApprovalProcess();
        xzApprovalProcess.setTempletBaseId(templetBaseId);
        xzApprovalProcess.setDeleteTime(newtime);
        int c=approvalProcessService.deleteNodeByKey(xzApprovalProcess);

        System.out.println("a:"+a+",b:"+b+",c:"+c);
        if(a>1 || b>1 || c>1){
            System.out.println("删除成功");
        }

        return mv;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////

    //添加模板页面的保存方法
    @RequestMapping("/addTemplet.emp")
    public ModelAndView addTemplet(@Validated({XzTempletBase.F1.class}) XzTempletBase xzTempletBase, BindingResult result){
        ModelAndView mv=new ModelAndView("redirect:/XzTempletBase/selTempletAll.emp");
        if(result.hasErrors()){
//            验证失败
            mv.setViewName("backEnd/templetCreate");
            mv.addObject("editType","add");
        }else {
            //根据模板名称查询模板的ID
            String templetName =xzTempletBase.getTempletName();
         //   XzTempletBase templetBase=xzTempletBaseService.selAllByName(templetName);
            Long templetBaseId = null;
            int a=0;
            int b = 0;
            int c = 0;

            if (templetName != null && !"".equals(templetName)) {
                if (xzTempletBaseService.selAllByName(templetName) != null) {
                    mv.addObject("templetNameCheck", "模板名称已经存在");
                    //验证失败
                    mv.setViewName("backEnd/templetCreate");
                } else {

                    //添加基础模板（添加模板名称）
                    a = xzTempletBaseService.insert(xzTempletBase);
                    //添加扩展模板
                   // XzTempletExtend xzTempletExtend=new XzTempletExtend();
                    templetBaseId = xzTempletBaseService.selByName(templetName);
                    //xzTempletExtend.setTempletBaseId(templetBaseId);
                    xzTempletBase.setTempletBaseId(templetBaseId);
                    b = templetExtendService.insert(xzTempletBase);
                    //添加节点到节点表（审批流程表）
                    c = approvalProcessService.addApprovalProcess(xzTempletBase);

                    mv.addObject("editType","add");
                }
            }
            //添加该模板的审批节点
            if (a == 1 && b == 1 && c ==1) {
                System.out.println("添加成功");
            }
        }

        return mv;
    }

    //跳到修改页面
    @RequestMapping("/toUpdatePage.emp")
    public ModelAndView toUpdatePage(Long templetBaseId){
        ModelAndView mv=new ModelAndView("backEnd/templetCreate");
        //查询模板基础表
        XzTempletBase xzTempletBase=new XzTempletBase();
        //获取基础模板的属性
        xzTempletBase=xzTempletBaseService.selAllById(templetBaseId);
        //获取基础模板中的扩展属性表
        xzTempletBase.setExtendObjectList(templetExtendService.selAllByBaseId(templetBaseId));
        //获取模板中的节点属性表
        xzTempletBase.setNodeObjectList(approvalProcessService.selByTempletBaseId(templetBaseId));
        //赋值给原由的模板名称
        xzTempletBase.setOleTempletName(xzTempletBase.getTempletName());

        System.out.println("xzTempletBase:"+xzTempletBase);
        //查询所有角色赋给select框
        XzRole role=new XzRole();
        List<XzRole> roleslist=RoleService.selRoleAll(role);



        mv.addObject("roleslist",roleslist);
//        mv.addObject("extendObjectList",xzTempletBase.getExtendObjectList());
//        mv.addObject("nodeObjectList",xzTempletBase.getNodeObjectList());
        mv.addObject("xzTempletBase",xzTempletBase);
        mv.addObject("editType","update");
        return mv;
    }

    //修改模板名称时模板名称存在的情况下跳回本身的修改页面
    @RequestMapping("toUpdateNameCheckPage")
    public ModelAndView toUpdateNameCheckPage(Long templetBaseId,String UpdateOfSaveCheck){
        ModelAndView mv=new ModelAndView("backEnd/templetCreate");
        System.out.println("toUpdateNameCheckPage方法中，templetBaseId:"+templetBaseId+",UpdateOfSaveCheck:"+UpdateOfSaveCheck);
        //查询模板基础表
        XzTempletBase xzTempletBase=new XzTempletBase();
        //获取基础模板的属性
        xzTempletBase=xzTempletBaseService.selAllById(templetBaseId);
        //获取基础模板中的扩展属性表
        xzTempletBase.setExtendObjectList(templetExtendService.selAllByBaseId(templetBaseId));
        //获取基础模板中的节点属性表
        xzTempletBase.setNodeObjectList(approvalProcessService.selByTempletBaseId(templetBaseId));
        //赋值给原由的模板名称
        xzTempletBase.setOleTempletName(xzTempletBase.getTempletName());
        //赋值给新的模板名称
        xzTempletBase.setTempletName("");
        System.out.println("xzTempletBase:"+xzTempletBase);
        //查询所有角色赋给select框
        XzRole role=new XzRole();
        List<XzRole> roleslist=RoleService.selRoleAll(role);

        System.out.println("UpdateOfSaveCheck:"+UpdateOfSaveCheck);

        if(UpdateOfSaveCheck.equals("repeatName")){
            mv.addObject("templetNameCheck", "模板名称已经存在");
        }

        mv.addObject("roleslist",roleslist);
//        mv.addObject("extendObjectList",xzTempletBase.getExtendObjectList());
//        mv.addObject("nodeObjectList",xzTempletBase.getNodeObjectList());
        mv.addObject("xzTempletBase",xzTempletBase);
        System.out.println("将跳到修改页面，xzTempletBase:"+xzTempletBase);
        mv.addObject("editType","update");
        return mv;
    }



    //修改模板的方法
    @RequestMapping("/updateTemplet.emp")
    public ModelAndView updateTemplet(XzTempletBase xzTempletBase){
       ModelAndView mv=new ModelAndView("redirect:/XzTempletBase/selTempletAll.emp");
       //做修改的模板名称是否已经存在的判断
        if (xzTempletBaseService.selAllByName(xzTempletBase.getTempletName()) != null  && !xzTempletBase.getTempletName().equals(xzTempletBase.getOleTempletName())) {
            mv.addObject("templetNameCheck", "模板名称已经存在");
            //验证失败
//            mv.setViewName("backEnd/templetCreate");
            XzTempletBase  checkOfTempletBase=xzTempletBaseService.selAllByName(xzTempletBase.getOleTempletName());
            System.out.println("跳到toUpdateNameCheckPage前，TempletBaseId："+checkOfTempletBase.getTempletBaseId());
            mv.setViewName("redirect:/XzTempletBase/toUpdateNameCheckPage.emp?templetBaseId="+checkOfTempletBase.getTempletBaseId()+"&UpdateOfSaveCheck=repeatName");
        }else {

            //修改基础模板的名称
            int a=xzTempletBaseService.updateTempletBase(xzTempletBase);

            Date newtime = new Date();  //给删除的信息添加删除时间
            //取出原有的扩展模板表信息id，从数据库中删除，重新再添加（此处做伪删除）
            int b = 0;
            int bb = 0;
            int bbb = 0;
            List<XzTempletExtend> extendList = xzTempletBase.getExtendObjectList();
            //删除扩展表
            if(templetExtendService.selAllByBaseId(xzTempletBase.getTempletBaseId()).size() >0) {
                XzTempletExtend xzTempletExtend = new XzTempletExtend();
                xzTempletExtend.setTempletBaseId(xzTempletBase.getTempletBaseId());
                xzTempletExtend.setDeleteTime(newtime);
                b = templetExtendService.deleteExtendByKey(xzTempletExtend);
            }
            //原有的扩展模板的信息再添加的到数据库（此处为用户在原由的input位置上添加的新信息）
            List<XzTempletExtend> extendObjectList=xzTempletBase.getExtendObjectList();
            System.out.println("extendObjectList:"+extendObjectList);
            if(extendObjectList != null) {
                bb = templetExtendService.updateOfinsert(xzTempletBase);
            }
            //修改时新添加的input框中的内容
            List ExtendTypeList=xzTempletBase.getExtendTypeList();
            System.out.println("extendObjectList:"+ExtendTypeList.size());
            if(ExtendTypeList.size() >0) {
                bbb = templetExtendService.insert(xzTempletBase);
            }

            //取出原有的节点模板表信息id，从数据库中删除，重新再添加（此处做伪删除）
            int c = 0;
            int cc = 0;
            int ccc = 0;
            //删除节点表
            if(approvalProcessService.selByTempletBaseId(xzTempletBase.getTempletBaseId()).size()>0) {
                XzApprovalProcess xzApprovalProcess = new XzApprovalProcess();
                xzApprovalProcess.setTempletBaseId(xzTempletBase.getTempletBaseId());
                xzApprovalProcess.setDeleteTime(newtime);
                c = approvalProcessService.deleteNodeByKey(xzApprovalProcess);
                System.out.println("删除了" + c + "个");
            }
            //原有的节点模板的信息再添加的到数据库（此处为用户在原由的select位置上重选的新信息）
            List<XzApprovalProcess> nodeObjectList=xzTempletBase.getNodeObjectList();
            if(nodeObjectList !=null) {
                cc = approvalProcessService.updateOfinsert(xzTempletBase);
                System.out.println("cc:" + cc);
            }
            //修改时新添加的input框中的内容
            List approvalProcessList=xzTempletBase.getNodeList();
            if(approvalProcessList.size() >0) {
                ccc = approvalProcessService.addApprovalProcess(xzTempletBase);
            }

        }
       return mv;
    }
    //删除扩展表的单条数据的方法(用ajax)
    @ResponseBody
    @RequestMapping("/deleteOneExtend.emp")
    public Map<String,Object> deleteOneExtend(Long id) {
        Map<String,Object> map=new HashMap<>();
        Date newtime = new Date();  //给删除的信息添加删除时间
        XzTempletExtend xzTempletExtend=new XzTempletExtend();
        xzTempletExtend.setTempletExtendId(id);
        xzTempletExtend.setDeleteTime(newtime);
        int i=templetExtendService.deleteByKey(xzTempletExtend);
        if(i==1) {
            map.put("deleteExtendResult", "成功");
        }else {
            map.put("deleteResult", "失败");
        }
        System.out.println("map："+map);
        return map;
    }
    //删除节点表中的单条数据的方法
    @ResponseBody
    @RequestMapping("/deleteOneNode.emp")
    public Map<String,Object> deleteOneNode(Long id){
        Map<String,Object> map=new HashMap<>();
        Date newtime = new Date();  //给删除的信息添加删除时间
        XzApprovalProcess xzApprovalProcess=new XzApprovalProcess();
        xzApprovalProcess.setApprovalprocessId(id);
        xzApprovalProcess.setDeleteTime(newtime);
        int i=approvalProcessService.deleteByKey(xzApprovalProcess);
        if(i==1){
            map.put("deleteNodeResult", "成功");
        }else {
            map.put("deleteNodeResult", "失败");
        }
        System.out.println("map:"+map);
        return map;
    }





}
